$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        dom: {
            schoolId: null,
            userType: null,
            viewBtn: null,
            appView: null,
            downAll: null
        },
        init: function () {
            this.dom.userType = $('#userType');
            this.dom.viewBtn  = $('#bt_view');
            this.dom.appView  = $('#school_app');
            this.dom.downAll  = $('#bt_down_all');
            this.dom.schoolId = $('#schoolId');
            this.dom.schoolId.select2({placeholder: '选择学校'});

            this.listener();
        },
        listener: function () {
            var $this = this;
            //查看学校应用
            $this.dom.viewBtn.click(function () {
                var schoolId = $this.dom.schoolId.val();
                if (!schoolId) {
                    window.top.xhTop.showMessage({type: 'error', title: '请选择学校！'});
                    return false;
                }
                $this.funObj().viewSchoolApp(schoolId, $this.dom.userType.val());
            });
            //批量下载
            $this.dom.downAll.click(function () {
                $this.funObj().downAllApp();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                viewSchoolApp: function (schoolId, userType) {
                    xhExtra.setBtnLoading($this.dom.viewBtn);
                    xhExtra.ajaxRequest('Appserver/getSchoolApp', {schoolId: schoolId, userType: userType}, {}, function (result) {
                        var bodyStr = '';
                        $.each(result, function (index, row) {
                            bodyStr += '<tr><td>' + row.appName + '</td><td>' + row.userType + '</td><td>' + row.versionCode + '</td>' +
                                '<td>' + row.versionName + '</td><td><a href="' + row.downloadUrl + '">下载</a></td></tr>';
                        });
                        xhExtra.setBtnNormal($this.dom.viewBtn);
                        $this.dom.appView.html(bodyStr);
                    });
                },
                downAllApp: function () {
                    var downList = $this.dom.appView.find('a');
                    $.each(downList, function (index, dom) {
                        window.open(dom.href);
                    });
                }
            }
        }
    }
}
